﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Transactions;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ProtoBuf;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        private string _stringToDraw = "1234567890qwertyuiop";



        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DrawTextWithAspose();
            DrawTextWithImageDraw();
            ToastNotification.ToastFont = new Font("Tahoma", 18);
            ToastNotification.Show(this, "测试", eToastPosition.MiddleCenter);
        }


        private void DrawTextWithAspose()
        {
            var image = Aspose.Imaging.Image.Load("image.jpg");
            var g = new Aspose.Imaging.Graphics(image);
            var font = new Aspose.Imaging.Font("Tahoma", 20);
            var brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Yellow);
            g.DrawString(_stringToDraw, font, brush, 0, 0);
            image.Save("asposeDrawn.jpg");
            image.Dispose();

        }

        private void DrawTextWithImageDraw()
        {
            var imageDrawn = new Neodynamic.SDK.ImageDraw.ImageDraw();
            imageDrawn.Canvas.AutoSize = true;

            var image = new Neodynamic.SDK.ImageDraw.ImageElement();
            image.SourceFile = "image.jpg";
            imageDrawn.Elements.Add(image);

            var font = new Neodynamic.SDK.ImageDraw.Font("Tahoma", 20);
            var textElement = new Neodynamic.SDK.ImageDraw.TextElement(_stringToDraw, font, Color.Yellow, Color.Black, 0);
            imageDrawn.Elements.Add(textElement);
            imageDrawn.Save("imageDraw Save.jpg");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            throw new Exception("dfdfd");
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            //MessageBox.Show("Test");
        }

        private void button3_Click(object sender, EventArgs e)
        {

            using (var client = GetClient())
            {
                var sub = new RedisSubscription(client.NativeClient);
                var dt = DateTime.Now;
                for (int i = 0; i < 1000; i++)
                {
                    var p = new People() { Age = i, Data = new byte[3 * 1024], Name = i.ToString() };
                    client.Lists["people"].Add(p);
                }
                System.Diagnostics.Debug.WriteLine((DateTime.Now - dt).TotalMilliseconds / 1000);
            }
        }

        private static RedisTypedClient<People> GetClient()
        {
            return new RedisTypedClient<People>(new RedisClient());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (var client = GetClient())
            {
                var dt = DateTime.Now;
                var lst = client.Lists["people"].GetAll();
                System.Diagnostics.Debug.WriteLine(lst.Count + " " + (DateTime.Now - dt));
                var sz = lst.Sum(p => p.Data.Length);
                System.Diagnostics.Debug.WriteLine("size: " + sz / (1024 * 1024));
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (var client = GetClient())
            {
                client.Lists["people"].Clear();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var p = new Person() {CaptureTime = DateTime.Now, Image = new byte[256*1024], Name = "Mike", Age = 34};

            var buf = Helper.Serialize(p);

            var cp = Serializer.DeepClone(p);

            var pp = Helper.Deserialize<Person>(buf);

        }
    }
}
